package components;

import common.Constant;
import common.Singleton;
import operator.Handler;
import platform.MeanLabel;
import pri.swg.Fader;
import pri.swg.Slider;
import view.Main;
import view.ViewControl;

import javax.swing.*;
import javax.swing.border.LineBorder;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

/**
 * 负责各处按钮的样式以及事务命令分发
 *
 * @author 柴家琪
 *
 */
public class MButton extends JLabel {
    private static final long serialVersionUID = 1L;
    public static final int OPEN_EXISTING = 0, OPEN = 1, SAVE = 2, IMPORT = 3, Add_mean = 4, Add_mean_apart = 41,
            Combine = 5, Apart = 6, Revoke = -11, Revoke_last = -1, Add_SelfAttr = 7, Add_Loc = 8, Reset_Main = -2,
            Delete = 9, Modify = 12, Confirm = 100, SAVE_EXCEL = 21, IMPORT_EXCEL = 31, REVERT = -100;
    private int enable = 1;
    private Slider slider;
    // private Handler handler;
    private ViewControl vc;
    private Fader bc, fc;
    public int order;

    private MButton() {
        vc = Singleton.vc();
    }

    /**
     *
     * 意向右键按钮
     */
    public MButton(String title, int order, Color aim, int size, Slider slider) {
        this(title, order, aim, size, true);
        this.slider = slider;
    }

    /**
     * 属性显示板上的确定、撤销按钮
     */
    public MButton(boolean confirm, Main m) {
        this();
        setText(confirm ? "确定" : "撤销");
        setForeground(Color.white);
        setBackground(Color.BLACK);
        setFont(new Font(Constant.roundfaceName, Font.BOLD, 18));
        Fader.fade(this, Fader.BACK_GROUND, 150, Fader.LISTEN_MouseEnter,
                confirm ? new Color(0, 127, 255) : Color.ORANGE);
        universalSet();
        addMouseListener(new MouseAdapter() {
            public void mouseReleased(MouseEvent e) {
                if (confirm) {
                    String attributes[] = new String[m.attrsName.length];
                    for (int i = 0; i < attributes.length; i++) {

                        attributes[i] = m.attrOptions[i].getFieldText();
                    }
                    vc.preMean.setAttribute(attributes);
                    Singleton.handler().modified();

                    vc.preMean.changeMeanBorder();
                    if (vc.preMean.getInner_id() != 0)
                        vc.preMean.getMain().changeMeanBorder();
                }

                // 刷新
                vc.showShower(vc.preMean);
                vc.showConfirmer(false);
            }
        });
    }

    /**
     * Home page button
     */
    public MButton(String title, int order, Color aim, String iconName) {
        this(title, order, aim, 20, false);
        this.setIcon(Singleton.resource().getIcon("home_" + iconName + ".png"));
    }

    /**
     * 内层按钮
     */
    public MButton(String title, int order, Color aim, int size, boolean inside) {
        this();
        Handler handler = Singleton.handler();

        this.order = order;
        // this.handler=handler;
        setText(title);
        setFont(new Font(Constant.boldfaceName, Font.BOLD, size));
        setBackground(new Color(0, 0, 0));
        setForeground(Color.WHITE);
        universalSet();

        if (inside) {
            bc = Fader.fade(this, Fader.BACK_GROUND, 200, Fader.RETURN, aim);
            fc = Fader.fade(this, Fader.FORE_GROUND, 200, Fader.RETURN, Color.black);
        } else {
            fc = Fader.fade(this, Fader.FORE_GROUND, 200, Fader.RETURN, aim);
        }

        addMouseListener(new MouseAdapter() {
            public void mouseEntered(MouseEvent e) {
                if (enable == 1) {
                    if (inside)
                        bc.goOn();
                    fc.goOn();
                }
            }

            public void mouseExited(MouseEvent e) {
                if (enable == 1) {
                    if (inside)
                        bc.goBack();
                    fc.goBack();
                    setBorder(null);
                }
            }

            public void mousePressed(MouseEvent e) {
                if (inside)
                    setBorder(new LineBorder(Color.white, 2, true));
                else
                    setFont(new Font(Constant.boldfaceName, Font.BOLD, size + 2));
            }

            public void mouseReleased(MouseEvent e) {
                setBorder(null);
                setFont(new Font(Constant.boldfaceName, Font.BOLD, size));
                boolean reset = true;
                if (e.getButton() == 1 && enable == 1) {
                    switch (order) {
                        case OPEN:
                            Singleton.docOperator().readDoc();
                            break;
                        case OPEN_EXISTING:
                            Singleton.xDocOperator().readXDoc();
                            break;
                        case IMPORT:
                            Singleton.xmlOperator().readXML();
                            break;
                        case IMPORT_EXCEL:
                            Singleton.xlsOperator().readExcel();
                            break;
                        case SAVE:
                            Singleton.fUtil().writeAll();
                            Singleton.main().saveSet();
                            break;
                        case SAVE_EXCEL:
                            Singleton.xlsOperator().writeExcel(true);
                            break;
                        case REVERT:

                            break;
                        case Combine:
                            callBack();
                            vc.showSelector(true);
                            break;
                        case Apart:
                            handler.apart();
                            vc.attr_addAll();
                            break;
                        case Revoke:
                            handler.recoveryTextColor(); // 恢复文本
                            callBack(); // 召回菜单
                            vc.reSetOrder(); // 重置当前选择
                            if (vc.lastOrder == Add_Loc)
                                locAddOver();
                            break;
                        case Revoke_last:
                            handler.revoke_last();
                            break;
                        case Add_mean:
                            callBack();
                            handler.addNewMainMean();
                            handler.recoveryTextColor();// 恢复
                            break;
                        case Add_mean_apart:
                            callBack();
                            handler.recoveryTextColor();
                            break;
                        case Add_Loc:
                            handler.posAdding = true;
                            vc.showMenus_Revoke();
                            vc.unableAttr();
                            break;
                        case Delete:
                            String title = "";
                            switch (vc.preSelected.type) {
                                case MeanLabel.MainMeanTop:
                                case MeanLabel.ViceMeanTop:
                                    title = "确认删除意象[ " + vc.preSelected.getText() + "] ？";
                                    break;
                                case MeanLabel.Location:
                                    vc.callBackAll();
                                    vc.showSelector(false);
                            }
                            if (!title.equals(""))
                                new MDialog(title, MDialog.DELETE);
                            break;
                        case Confirm:
                            switch (vc.preSelected.type) {
                                case MeanLabel.MainMeanTop:// 添加
                                case MeanLabel.ViceMeanTop:
                            }
                            break;
                    }
                    if (vc != null && reset)
                        vc.lastOrder = order;
                }
            }
        });
    }


    private void universalSet() {
        setHorizontalAlignment(JLabel.CENTER);
        setOpaque(true);
        setCursor(new Cursor(Cursor.HAND_CURSOR));
    }

    private void callBack() {
        slider.goBack();
    }

    private void locAddOver() {
        Singleton.handler().posAdding = false;
        vc.ableAttr();
    }

    /**
     * 激活按钮
     */
    public void toEnable() {
        setForeground(Color.WHITE);
        setEnabled(true);
        enable = 1;
        setCursor(new Cursor(Cursor.HAND_CURSOR));
    }

    /**
     * 使按钮失效
     */
    public void toUnEnable() {
        setEnabled(false);
        enable = 0;
        setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
    }
}